
/**
 * 
 */
$(document).ready(function() {

	$("form").submit(function() {
		return false;
	});

	var VAL = {
		EVT_USR : $("#userId").text(),
		T_FDPAPLYSO : "FDPAPLYSO",
		T_FBPMDLDEF : "FBPMDLDEF",
		T_FBPBISCUS : "FBPBISCUS",
		T_FBPBISBOM : "FBPBISBOM",
		NORMAL : "0000000",
		DISABLED_ATTR : {
			"disabled" : true
		},
		ENABLED_ATTR : {
			"disabled" : false
		}
	};

	var domObj = {
		W : $(window),
		$woId : $("#woId"),
		$soId : $("#soId"),
		$woCate : $("#woCate"),
		$cusIdFk : $("#cusIdFk"),
		$mdlIdFk : $("#mdlIdFk"),
		$plnPrdQty : $("#plnPrdQty"),
		$plnStbDatepicker : $("#plnStbDatepicker"),
		$plnCompDatepicker : $("#plnCompDatepicker"),
		$woNote : $("#woNote"),
		$woDsc : $("#woDsc"),
		
		buttons : {
			$query_btn : $("#query_btn"),
			$save_btn : $("#save_btn"),
			$update_btn : $("#update_btn"),
			$del_btn : $("#del_btn"),
			$clear_btn : $("#clear_btn")
		},

		grids : {
            $UnderDiv    : $(".UnderDiv"),
            $cusOrderDiv : $("#cusOrderDiv"),
			$cusOrderGrd : $("#cusOrderGrd")
		}
	};
    //jqGrid表格自适应
    function resizeFnc(){
        var grdDivHeight,grdDivWidth;
        grdDivWidth = domObj.W.width() - 60;
        grdDivHeight = domObj.W.height() - domObj.grids.$UnderDiv.offset().top;

        domObj.grids.$UnderDiv.height(grdDivHeight);
        domObj.grids.$cusOrderDiv.width(grdDivWidth);
        domObj.grids.$cusOrderDiv.height(grdDivHeight*0.99 - 51);
        domObj.grids.$cusOrderGrd.setGridWidth(grdDivWidth*0.99);
        domObj.grids.$cusOrderGrd.setGridHeight(grdDivHeight*0.95 - 101);
    };
    domObj.W.resize(function() {
        resizeFnc();
    });
	function initCusOrder() {
		domObj.grids.$cusOrderGrd.jqGrid({
			url : "",
			datatype : "local",
			mtype : "POST",
			autowidth : true,
            shrinkToFit:false,
            scroll: true,
			resizable : true,
			loadonce : true,
			viewrecords : true,
			rownumbers : true,
			emptyrecords : true,
			colModel : [ {
				name : 'crt_timestamp',
				index : 'crt_timestamp',
				label : CRT_TIMESTAMP,
				width : 180
			},{
				name : 'so_id',
				index : 'so_id',
				label :CUS_SO_ID_TAG ,
				width : 180
			}, {
                name : 'wo_id',
                index : 'wo_id',
                label : SO_ID_TAG,
                width : 180
            },{
				name : 'cus_id_fk',
				index : 'cus_id_fk',
				label : CUS_ID_TAG,
				width : 190
			}, {
				name : 'mdl_id_fk',
				index : 'mdl_id_fk',
				label : MDL_ID_TAG,
				width : 190
			}, {
				name : 'pln_prd_qty',
				index : 'pln_prd_qty',
				label : PLN_PRD_QTY_TAG,
				width : 160
			}, {
				name : 'pln_stb_timestamp',
				index : 'pln_stb_timestamp',
				label : PLN_STB_TIMESTAMP_TAG,
				width : 150
			}, {
				name : 'pln_cmp_timestamp',
				index : 'pln_cmp_timestamp',
				label : PLN_CMP_TIMESTAMP_TAG,
				width : 150
			}, {
                name : 'wo_dsc',
                index : 'wo_dsc',
                label : SO_DSC_TAG,
                width : 150
            }, {
                name : 'wo_note',
                index : 'wo_note',
                label : REMARK_TAG,
                width : 150
            }],
			pager : "cusOrderPg"
		});
	};

	//将控件变成都可修改
	function enableFn() {
        domObj.$soId.attr(VAL.ENABLED_ATTR);
		domObj.$woCate.attr(VAL.ENABLED_ATTR);
		domObj.$cusIdFk.attr(VAL.ENABLED_ATTR);
		domObj.$mdlIdFk.attr(VAL.ENABLED_ATTR);
		domObj.$woNote.attr(VAL.ENABLED_ATTR);
		domObj.$woDsc.attr(VAL.ENABLED_ATTR);
		domObj.$plnPrdQty.attr(VAL.ENABLED_ATTR);
	};
	
	// 初始化化下拉框函数
	var initSelectFnc = {
		// 1.初始化订单类型
		initOrderType : function() {
			comAddValueByDataCateFnc("#woCate", "WOCT", "data_ext","data_desc",true);
		},
		// 2.初始化 客户代码
		iniCusIdSel : function(cusIdObj) {
			var inObj, outObj;
			var iary = {
				cus_typ : 'R'
			};

			inObj = {
				trx_id : VAL.T_FBPBISCUS,
				action_flg : 'Q',
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom .addSelectArr(domObj.$cusIdFk, outObj.oary, "cus_id","cus_id","",true);
			}
		},

		// 4.产品代码
		initmdlId : function() {
			var inObj, outObj, i, tblCnt, oary;
            var iary = {
                valid_flg :"Y"
            }
            inObj = {
                trx_id : VAL.T_FBPMDLDEF,
                action_flg : "Q",
                iary : [ iary ]
            };

            outObj = comTrxSubSendPostJson(inObj);
            var tblCnt = outObj.tbl_cnt;
            if (outObj.rtn_code === VAL.NORMAL && tblCnt>0) {
                SelectDom .addSelectArr(domObj.$mdlIdFk, outObj.oary, "mdl_id","mdl_id","",true);
            }
		},
		// 5.计划投产日期 //6.交货日期
		iniDateTimePicker : function() {
			var datepickerData;
			domObj.$plnStbDatepicker.datetimepicker({
				language : 'zh-CN',
				autoclose : true,
				pickTime : false
			});
			domObj.$plnStbDatepicker.datetimepicker().on(
					'changeDate',
					function(ev) {
						domObj.$plnStbDatepicker
								.datetimepicker('hide');
					});

			domObj.$plnCompDatepicker.datetimepicker({
				language : 'zh-CN',
				autoclose : true,
				pickTime : false
			});
			domObj.$plnCompDatepicker.datetimepicker().on(
					'changeDate',
					function(ev) {
						domObj.$plnCompDatepicker
								.datetimepicker('hide');
					});

			datepickerData = domObj.$plnStbDatepicker.data("datetimepicker");
			datepickerData.setLocalDate(datepickerData.getLocalDate());
			datepickerData = domObj.$plnCompDatepicker.data("datetimepicker");
			datepickerData.setLocalDate(datepickerData.getLocalDate());
		}
	};

	function init() {
		initSelectFnc.initOrderType();
		initSelectFnc.iniCusIdSel();
        initSelectFnc.initmdlId();
		initSelectFnc.iniDateTimePicker();
	};

	var baseFnc = {
		convertTimestamp2Date : function(timestamp) {
			var date = new Date(timestamp.substring(0, 10));
			/*
			 * date.setHours(timestamp.substring(11, 13));
			 * date.setMinutes(timestamp.substring(14, 16));
			 * date.setSeconds(timestamp.substring(17, 19));
			 */
			return date;
		}
	};

	var buttonFnc = {
		queryFnc : function() {
			var soId, woCate, cusIdFk, mdlIdFk, iary, inObj, outObj;
            soId = $.trim(domObj.$soId.val());
			woCate = $.trim(domObj.$woCate.val());
			cusIdFk = $.trim(domObj.$cusIdFk.val());
			mdlIdFk = $.trim(domObj.$mdlIdFk.val());
			iary = {
				so_id : soId,
				wo_cate : woCate,
				cus_id_fk : cusIdFk,
				mdl_id_fk : mdlIdFk
			};
			inObj = {
				trx_id : VAL.T_FDPAPLYSO,
				action_flg : "Q",
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#cusOrderGrd");
			}
		},

		saveFnc : function() {
			var actionFlg, inObj, outObj, iary, newRowId, rowIds, selRowId;
			var woId, soId, woCate, cusIdFk, mdlIdFk, plnPrdQty, woNote;
			var plnStbTimestamp, plnCompTimestamp, woDsc;
			
			woId = $.trim(domObj.$woId.val());
			soId = $.trim(domObj.$soId.val());
			woCate =$.trim(domObj.$woCate.val());
			cusIdFk =$.trim(domObj.$cusIdFk.val());
			mdlIdFk =$.trim(domObj.$mdlIdFk.val());
			plnPrdQty =$.trim(domObj.$plnPrdQty.val());
			woNote =$.trim(domObj.$woNote.val());
			woDsc =$.trim(domObj.$woDsc.val());
			plnStbTimestamp = domObj.$plnStbDatepicker.data("datetimepicker").getLocalDate().format('yyyy-MM-dd');
			plnCompTimestamp = domObj.$plnCompDatepicker.data("datetimepicker").getLocalDate().format('yyyy-MM-dd');
	
			//内部订单号有值说明是修改，无值为新增
			actionFlg = woId ? "U" : "A";

			// 判断为空 ，和时间问题
            if (!soId) {
                showErrorDialog("", "客户订单号不能为空，请输入!");
                return false;
            }
			if (!woCate) {
				showErrorDialog("", "订单种类不能为空,请选择!");
				return false;
			}
			if (!cusIdFk) {
				showErrorDialog("", "客户代码不能为空,请选择!");
				return false;
			}
			if (!mdlIdFk) {
				showErrorDialog("", "产品代码不能为空,请选择!");
				return false;
			}
			if (!plnPrdQty) {
				showErrorDialog("", "订单数量不能为空,请输入!");
				return false;
			} else if (!ComRegex.isInt(plnPrdQty)){
                showErrorDialog("","订单数量不是有效的整数");
                return false;
            }

			if (!plnStbTimestamp) {
				showErrorDialog("", "请选择计划投产日期!");
				return false;
			}
			if (!plnCompTimestamp) {
				showErrorDialog("", "请选择计划完成日期!");
				return false;
			}
			if (baseFnc.convertTimestamp2Date(plnStbTimestamp) > baseFnc
					.convertTimestamp2Date(plnCompTimestamp)) {
				showErrorDialog("", "计划投产日期不能在计划完成日期之后!");
				return false;
			}
			if (!woDsc) {
				showErrorDialog("", "订单说明不能为空,请输入!");
				return false;
			}
			iary = {
				wo_id : woId,
                so_id : soId,
				wo_cate : woCate,
				cus_id_fk : cusIdFk,
				mdl_id_fk : mdlIdFk,
				pln_prd_qty : plnPrdQty,
				pln_stb_timestamp : plnStbTimestamp,
				pln_cmp_timestamp : plnCompTimestamp,
				wo_note : woNote,
				wo_dsc : woDsc
			};
			// 输给后台的数据
			inObj = {
				trx_id : VAL.T_FDPAPLYSO,
				action_flg : actionFlg,
				iary : [ iary ],
				evt_usr : VAL.EVT_USR,
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				if (actionFlg == "A") {
					newRowId = getGridNewRowID("#cusOrderGrd")
					domObj.grids.$cusOrderGrd.jqGrid("addRowData", newRowId,outObj.oary[0]);
					domObj.grids.$cusOrderGrd.setSelection(newRowId, true); 
					showSuccessDialog("客户订单新建成功");

				} else if (actionFlg == "U") {
					selRowId = domObj.grids.$cusOrderGrd.jqGrid("getGridParam", "selrow");
					domObj.grids.$cusOrderGrd.jqGrid("setRowData", selRowId,outObj.oary[0]);
					showSuccessDialog("客户订单信息更新成功");
				}
			}
		},

		updateFnc : function() {
			var selRowId, rowData, woId, inObj;
			
			$("input").val("");
			$("textarea").val("");
            SelectDom.setSelect(domObj.$woCate, "");
            SelectDom.setSelect(domObj.$cusIdFk,"");
            SelectDom.setSelect(domObj.$mdlIdFk,"");
            
			selRowId = domObj.grids.$cusOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要修改的客户订单");
				return false;
			}
            rowData = domObj.grids.$cusOrderGrd.jqGrid("getRowData", selRowId);
            woId = rowData.wo_id;
			iary = {
				wo_id : woId
			}
			inObj = {
				trx_id : VAL.T_FDPAPLYSO,
				action_flg : 'Q',
				iary : [ iary ]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL && outObj.tbl_cnt>0) {
				var oary = outObj.oary[0];
				//检查产品代码是否存在
                var iary = {
                    mdl_id : oary.mdl_id_fk
                }
                var inObj_mdl = {
                    trx_id : VAL.T_FBPMDLDEF,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                var outObj_mdl = comTrxSubSendPostJson(inObj_mdl);
                if(outObj_mdl.rtn_code == VAL.NORMAL && outObj_mdl.tbl_cnt>0){
					;
				}else {
                    showErrorDialog("", "产品代码[" + oary.mdl_id_fk + "]在系统中不存在，请检查！");
                    return false;
                }
                //检查客户代码是否存在
                var iary = {
                    cus_id_fk : oary.cus_id_fk
                }
                var inObj_cus = {
                    trx_id : VAL.T_FBPBISCUS,
                    action_flg : "Q",
                    iary : [ iary ]
                };
                var outObj_cus = comTrxSubSendPostJson(inObj_cus);
                if(outObj_cus.rtn_code == VAL.NORMAL && outObj_cus.tbl_cnt>0){
                    ;
                }else {
                    showErrorDialog("", "客户代码[" + oary.cus_id_fk + "]在系统中不存在，请检查！");
                    return false;
                }
                
				domObj.$woId.val(oary.wo_id);
                domObj.$soId.val(oary.so_id);
				domObj.$plnPrdQty.val(oary.pln_prd_qty);
				domObj.$woNote.val(oary.wo_note);
				domObj.$woDsc.val(oary.wo_dsc);
				$("#plnStbDatepicker input").val(oary.pln_stb_timestamp);
				$("#plnCompDatepicker input").val(oary.pln_cmp_timestamp);
				SelectDom.setSelect(domObj.$woCate,oary.wo_cate);
				SelectDom.setSelect(domObj.$cusIdFk,oary.cus_id_fk);
				// 下面2个互换位置不能显示(setSelect 有change)
				SelectDom.setSelect(domObj.$mdlIdFk,oary.mdl_id_fk);
				domObj.$soId.attr(VAL.DISABLED_ATTR);
			}
		},

		delFnc : function() {
			var selRowId, rowData, iary, inObj, outObj;
			// 获得选中的行ID
			selRowId = domObj.grids.$cusOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要删除的客户订单");
				return false;
			}
			// 删除按钮绑定删除事件
			$("#del_btn").showCallBackWarnningDialog({
				errMsg : "是否删除该客户订单信息,请确认!!!!",
				callbackFn : function(data) {
					if (data.result == true) {
						// 获得选中行的数据
						rowData = domObj.grids.$cusOrderGrd.jqGrid("getRowData",selRowId);
						iary = {
							wo_id : rowData.wo_id,
						};
						inObj = {
							trx_id : VAL.T_FDPAPLYSO,
							action_flg : "D",
							iary : [ iary ]
						};
						outObj = comTrxSubSendPostJson(inObj);
						if (outObj.rtn_code === VAL.NORMAL) {
							showSuccessDialog("删除该客户订单成功");
							domObj.grids.$cusOrderGrd.jqGrid("delRowData",selRowId);
							// 删除完后页面除了Parameter信息，清空
						}
					}
				}
			});
		},

		clearFnc : function() {
//			$("#cusOrderGrd").jqGrid('clearGridData');
			enableFn();
			// 清空控件
			SelectDom.initWithSpace(domObj.$woCate);
			SelectDom.initWithSpace(domObj.$cusIdFk);
			SelectDom.initWithSpace(domObj.$mdlIdFk);
			domObj.$woId.val("");
            domObj.$soId.val("");
			domObj.$woNote.val("");
			domObj.$plnPrdQty.val("");
			domObj.$woDsc.val("");
			// 初始化下拉框
			init();
		}
	};

	//页面初始化函数
	function pageInit() {
		domObj.$woId.attr(VAL.DISABLED_ATTR);
//		$("#plnStbDatepicker input").attr(VAL.DISABLED_ATTR);
//		$("#plnCompDatepicker input").attr(VAL.DISABLED_ATTR);
		initCusOrder();
		resizeFnc();
		//输入文本框置空
		domObj.$woId.val("");
        domObj.$soId.val("");
		domObj.$plnPrdQty.val("");
		domObj.$woDsc.val("");
		domObj.$woNote.val("");
		
		//按钮绑定
		domObj.buttons.$query_btn.bind('click',buttonFnc.queryFnc);
		domObj.buttons.$save_btn.bind('click',buttonFnc.saveFnc);
		domObj.buttons.$update_btn.bind('click',buttonFnc.updateFnc);
		domObj.buttons.$del_btn.bind('click', buttonFnc.delFnc);
		domObj.buttons.$clear_btn.bind('click',buttonFnc.clearFnc);
		//初始化选择框
		init();
		enableFn();
	}
	
    pageInit();
    //enter
   /* document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
});
